/*
 * @Author: Qimu
 * @Date: 2020-04-20 13:38:40
 * @LastEditors: Qimu
 * @LastEditTime: 2020-04-22 21:41:17
 * @Description: file content
 * @FilePath: \koa2_api\controllers\carousel.js
 */
const Carousel = require('../module/schama/carousel');
const tools = require('../module/utlis/tools');
const query = require('../module/utlis/query');
const config = require('../module/config/config');

//轮播业务
class CarouselController {
  //轮播设置(POST)
  static async setCarousel(ctx) {
    ctx.status = 200;
    const {
      carousel_img, //轮播图片
      carousel_name,//轮播名称
      carousel_url,//轮播图片链接
      carousel_status,//是否显示轮播
    } = ctx.request.body;

    let data = {  //用户注册数据
      carousel_img,
      carousel_name,
      carousel_url,
      carousel_status,
    }
    let saveResult = await query.save(Carousel, data);//查找数据结果
    console.log(saveResult);
    ctx.body = {
      'code': -1,
      'data': {},
      'message': '数据上传保存成功!'
    }
  }

  //轮播列表(GET)
  static async listCarousel(ctx) {
    ctx.status = 200;
    let queryResult = await query.find(Carousel, {});//查找数据结果
    console.log(queryResult);
    if (queryResult.length > 0) {
      ctx.body = {
        'code': 1,
        'data': {
          'list': queryResult
        },
        'message': '轮播数据返回成功!'
      }
    } else {
      ctx.body = {
        'code': -1,
        'data': { 'list': [] },
        'message': '暂时没有轮播数据!'
      }
    }
  }
}

exports = module.exports = CarouselController;